﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Engine.SceneControl;
using Microsoft.Xna.Framework;

namespace Engine.IA.Steering
{
    public class CollisionAvoidance : ISteeringBehavior
    {
        IObject obj;
        TriVisibilitySensor vsensor;
        IWorld world;


        public CollisionAvoidance(IWorld world, IObject obj)
        {
            this.obj = obj;            
            vsensor = new TriVisibilitySensor(world, obj);
            //vsensor.Distance = 50;            
        }

        #region ISteeringBehavior Members

        public SteeringInfoOutput Perform(SteeringInfoInput input)
        {
            IList<VisibilitySensorInformation> vinfos = vsensor.DetectEnviroment();
            Vector3 resultande = Vector3.Zero;
            foreach (VisibilitySensorInformation item in vinfos)
            {
                if (item.Agente.Obj != input.Owner && item.Agente.Obj!=input.Target)
                  {
                      Vector3 dir = item.RayTestInfo.ImpactNormal;
                      dir.Normalize();
                      float scale = 40/item.RayTestInfo.Distance;
                      resultande += dir * scale;                                                                            
                  }

            }
            SteeringInfoOutput so = new SteeringInfoOutput();
            so.Direction = resultande;
            return so;
        }

        #endregion
    }
}
